<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="昵称/手机号">
            <a-input v-decorator="['search']" placeholder="请输入昵称/手机号" />
          </a-form-item>
          <!-- <a-form-item v-if="$module('user-grade')" label="会员等级">
            <a-select v-decorator="['gradeId', { initialValue: 0 }]">
              <a-select-option :value="0">全部</a-select-option>
              <a-select-option
                v-for="(item, index) in gradeList"
                :key="index"
                :value="item.grade_id"
              >{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item> -->
          <a-form-item label="注册时间">
            <a-range-picker format="YYYY-MM-DD" v-decorator="['betweenTime']" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="plus" @click="adduser">录入用户</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="user_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15">
      <!-- 会员头像 -->
      <span slot="avatar_url" slot-scope="text">
        <div class="avatar">
          <img v-if="text" width="45" height="45" :src="text" alt="用户头像" />
          <img v-else width="45" height="45" src="~@/assets/img/default-avatar.png" alt="用户头像" />
        </div>
      </span>
      <span slot="main_info" slot-scope="item">
        <p>{{ item.nick_name }}</p>
        <p class="c-p">{{ item.mobile }}</p>
      </span>
      <!-- 身份 -->
      <span slot="id_card" slot-scope="item">
        <template v-if="item.rule_id == 1">
          <!-- <p class="psstyle" >配送员</p> -->
          <a-tag class="psstyle">配送员</a-tag>
        </template>
        <span v-else>--</span>
      </span>
      <!-- 会员等级 -->
      <!-- <span slot="grade" slot-scope="text">
        <a-tag v-if="text">{{ text.name }}</a-tag>
        <span v-else>--</span>
      </span> -->
      <!-- 余额/积分 -->
      <!-- <span slot="balance" slot-scope="text, item">
        <p>
          <span>余额：</span>
          <span class="c-p">{{ text }}</span>
        </p>
        <p>
          <span>积分：</span>
          <span class="c-p">{{ item.points }}</span>
        </p>
      </span> -->
      <!-- 实际消费金额 -->
      <!-- <span slot="expend_money" slot-scope="text">
        <span class="c-p">{{ text }}</span>
      </span> -->
      <!-- 注册来源 -->
      <span slot="platform" class="platform" slot-scope="text">
        <!-- <platform-icon :name="text" :showTips="true" :iconSize="17" /> -->
        {{ text }}
      </span>
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="item">
        <!-- <a
          v-if="$module('market-recharge')"
          v-action:recharge
          @click="handleRecharge(item)"
          title="会员充值"
        >充值</a> -->
        <!-- <a v-if="$module('user-grade')" v-action:grade @click="handleGrade(item)" title="会员等级">等级</a> -->
        <a v-action:delete @click="handleDelete(item)"> 删除</a>
        <a v-action:delete @click="edituser(item)">编辑</a>
        <a v-action:delete v-if="item.rule_id === 0" @click="setworker(item)">设置为配送员</a>
        <a v-action:delete v-if="item.rule_id === 1" @click="setworker(item)">设置为用户</a>

      </span>
    </s-table>
    <GradeForm ref="GradeForm" :gradeList="gradeList" @handleSubmit="handleRefresh" />
    <RechargeForm ref="RechargeForm" @handleSubmit="handleRefresh" />
    <!-- 录入/编辑用户 -->
    <a-modal v-model="visible" @cancel="cancel" :title="mytitle" @ok="handleOk">
      <div class="inputbox">
        <!-- <p>姓名：</p> -->
        <a-input addon-before="姓名：" placeholder="请输入姓名" v-model="username" />
      </div>
      <div class="inputbox">
        <p>门店：</p>
        <a-select style="width: 100%;" v-model="zxc" @change="changeshop" >
          <a-select-option v-for="item in shopListTreeData" :key="item.store_id" :value="item.store_id">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </div>

      <div class="inputbox">
        <!-- <p>手机号：</p> -->
        <a-input addon-before="手机号：" placeholder="请输入手机号" v-model="phone" />
      </div>
    </a-modal>
  </a-card>
</template>

<script>
import { filterModules } from '@/core/app'
import * as shopApi from '@/api/store/user'
import * as Api from '@/api/user'
import * as GradeApi from '@/api/user/grade'
import { STable } from '@/components'
import PlatformIcon from '@/components/PlatformIcon'
import { GradeForm, RechargeForm } from './modules'

// table表头
const columns = filterModules([
  {
    title: '会员ID',
    dataIndex: 'user_id'
  },
  {
    title: '会员头像',
    dataIndex: 'avatar_url',
    scopedSlots: { customRender: 'avatar_url' }
  },
  {
    title: '昵称/手机号',
    scopedSlots: { customRender: 'main_info' }
  },
  {
    title: '身份',
    scopedSlots: { customRender: 'id_card' }
  },
  // {
  //   title: '会员等级',
  //   moduleKey: 'user-grade',
  //   dataIndex: 'grade',
  //   scopedSlots: { customRender: 'grade' }
  // },
  // {
  //   title: '余额/积分',
  //   dataIndex: 'balance',
  //   scopedSlots: { customRender: 'balance' }
  // },
  // {
  //   title: '实际消费金额',
  //   dataIndex: 'expend_money',
  //   scopedSlots: { customRender: 'expend_money' }
  // },
  {
    title: '录入人',
    dataIndex: 'platform',
    scopedSlots: { customRender: 'platform' }
  },
  {
    title: '录入时间',
    dataIndex: 'create_time'
  },
  {
    title: '操作',
    // dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
])

export default {
  name: 'Index',
  components: {
    STable,
    GradeForm,
    RechargeForm,
    PlatformIcon
  },
  data () {
    return {
      zxc: ' ',
      username: '',
      phone: '',
      mytitle: '',
      user_id: '',
      visible: false,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // table表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      },
      // 会员等级列表
      gradeList: [],
      shopListTreeData: [],
      store_id: ''
    }
  },
  created () {
    // 获取会员等级列表
    this.getGradeList()
    this.getshoplist()
  },
  methods: {
    // 获取门店列表
    getshoplist () {
      shopApi.getshop({})
        .then(result => {
          result.data.map(item => {
            item.name = item.store_name
          })
          this.shopListTreeData = result.data
        })
        .finally(result => {

        })
    },
    // 获取会员等级列表
    getGradeList () {
      GradeApi.all().then(result => {
        this.gradeList = result.data.list
      })
    },
    changeshop (e) {
      this.store_id = e
    },
    // 修改会员等级
    handleGrade (item) {
      this.$refs.GradeForm.handle(item)
    },

    // 会员充值
    handleRecharge (item) {
      this.$refs.RechargeForm.handle(item)
    },

    // 删除记录
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ userId: item.user_id })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },
    // 编辑用户
    edituser (item) {
      console.log(item, '我是用户信息')
      this.phone = item.mobile
      this.store_id = item.store_id
      this.username = item.nick_name
      this.user_id = item.user_id
      this.shopListTreeData.map(el => {
        if (item.store_id === el.store_id) {
          this.zxc = el.name
        }
      })
      this.mytitle = '编辑用户'
      this.visible = true
    },
    // 录入用户
    adduser () {
      this.mytitle = '录入用户'
      this.visible = true
    },
    // 关闭弹出层
    cancel () {
      console.log('我关闭了')
      this.phone = ''
      this.store_id = ''
      this.username = ''
      this.user_id = ''
      this.zxc = ''
    },
    // 确定修改或者录入客户
    handleOk (e) {
     if (!this.store_id) {
      this.$message.warning('请选择门店~')
      return false
     }
      this.visible = false
      const app = this
      if (app.mytitle === '编辑用户') {
        Api.editcustomer({ user_name: this.username, mobile: this.phone, user_id: this.user_id, store_id: this.store_id })
        .then(result => {
          app.$message.success(result.message, 1.5)
          app.handleRefresh()
          this.phone = ''
          this.store_id = ''
          this.username = ''
          this.user_id = ''
          this.zxc = ''
        })
      } else {
        Api.addcustomer({ user_name: this.username, mobile: this.phone, store_id: this.store_id })
        .then(result => {
          app.$message.success(result.message, 1.5)
          app.handleRefresh()
          this.phone = ''
          this.store_id = ''
          this.username = ''
          this.user_id = ''
          this.zxc = ''
        })
      }
    },
    // 设置为送水员
    setworker (item) {
      const app = this
      var str = ''
      if (item.rule_id === 0) {
        str = '配送员'
      } else {
        str = '普通用户'
      }
      const modal = this.$confirm({
        title: `您确定要设置该用户为${str}吗?`,
        onOk () {
          return Api.setworker({ userId: item.user_id, form: { rule_id: item.rule_id === 0 ? 1 : 0 } })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },
    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}

.psstyle {
  text-align: center;
  padding: 1px 10px;
  border: 1px solid #dd5050;
  border-radius: 5px;
  color: #dd5050;
}

.inputbox {
  display: flex;
  align-items: center;
  margin: 40px 0;

  p {
    flex-shrink: 0;
  }
}
</style>
